﻿using System;
using System.Windows.Forms;
using Landpy.CruiseControlNet.ConfigurationSidekick.Resources;
using Landpy.CruiseControlNet.FacadeInterface.ConfigurationModule;

namespace Landpy.CruiseControlNet.ConfigurationSidekick.ConfigTreeModule
{
    class TreeNodesBuilder
    {
        private CruiseControl CruiseControl { get; set; }
        public TreeNode CruiseControlTreeNode { get; private set; }

        public TreeNodesBuilder(CruiseControl cruiseControl)
        {
            this.CruiseControl = cruiseControl;
        }

        public TreeNode BuildCruiseControlNode()
        {
            if (this.CruiseControl != null)
            {
                this.CruiseControlTreeNode = new TreeNode()
                                                 {
                                                     Text = Resource.TreeNode_CruiseControl,
                                                     Name = Resource.TreeNode_CruiseControl,
                                                     Tag =
                                                         new TreeNodeIndentity(TreeNodeType.CruiseControl, String.Empty)
                                                 };
            }
            return this.CruiseControlTreeNode;
        }

        #region InternalSecurity

        #region Security

        public TreeNode BuildInternalSecurityNode(TreeNode cruiseControlTreeNode)
        {
            TreeNode internalSecurityTreeNode = null;
            if (this.CruiseControl.InternalSecurity != null)
            {
                internalSecurityTreeNode = new TreeNode()
                                                  {
                                                      Text = Resource.TreeNode_InternalSecurity,
                                                      Name = Resource.TreeNode_InternalSecurity,
                                                      Tag =
                                                          new TreeNodeIndentity(TreeNodeType.InternalSecurity,
                                                                                String.Empty)
                                                  };
                this.BuildSecurityUsersNode(internalSecurityTreeNode);
                this.BuildInternalPermissionsNode(internalSecurityTreeNode);
                cruiseControlTreeNode.Nodes.Add(internalSecurityTreeNode);
            }
            return internalSecurityTreeNode;
        }

        public TreeNode BuildSecurityUsersNode(TreeNode internalSecurityTreeNode)
        {
            TreeNode securityUsersTreeNode = null;
            if (this.CruiseControl.InternalSecurity.Users != null)
            {
                securityUsersTreeNode = new TreeNode()
                                            {
                                                Text = Resource.TreeNode_SecurityUsers,
                                                Name = Resource.TreeNode_SecurityUsers
                                            };
                foreach (var user in this.CruiseControl.InternalSecurity.Users)
                {
                    this.BuildSecurityUserNode(user, securityUsersTreeNode);
                }
                internalSecurityTreeNode.Nodes.Insert(0, securityUsersTreeNode);
            }
            return securityUsersTreeNode;
        }

        public TreeNode BuildSecurityUserNode(SecurityUser user, TreeNode securityUsersTreeNode)
        {
            var securityUserTreeNode = new TreeNode()
            {
                Text = String.Format(@"{0}[{1}]", Resource.TreeNode_SecurityUser, user.Name),
                Name = user.Name,
            };
            switch (user.Type)
            {
                case SecurityUserType.LdapUser:
                    securityUserTreeNode.Tag = new TreeNodeIndentity(TreeNodeType.LdapUser, user.Id.ToString());
                    break;
                case SecurityUserType.PasswordUser:
                    securityUserTreeNode.Tag = new TreeNodeIndentity(TreeNodeType.PasswordUser, user.Id.ToString());
                    break;
                case SecurityUserType.UserName:
                    securityUserTreeNode.Tag = new TreeNodeIndentity(TreeNodeType.UserName, user.Id.ToString());
                    break;
                default:
                    break;
            }
            securityUsersTreeNode.Nodes.Add(securityUserTreeNode);
            return securityUserTreeNode;
        }

        #endregion

        #region Permission

        public TreeNode BuildInternalPermissionsNode(TreeNode internalSecurityTreeNode)
        {
            TreeNode permissionsTreeNode = null;
            if (this.CruiseControl.InternalSecurity.Permissions != null)
            {
                permissionsTreeNode = new TreeNode()
                                              {
                                                  Text = Resource.TreeNode_Permissions,
                                                  Name = Resource.TreeNode_Permissions
                                              };

                foreach (var permission in this.CruiseControl.InternalSecurity.Permissions)
                {
                    this.BuildInternalPermissionNode(permission, permissionsTreeNode);
                }
                internalSecurityTreeNode.Nodes.Add(permissionsTreeNode);
            }
            return permissionsTreeNode;
        }

        public TreeNode BuildInternalPermissionNode(Permission permission, TreeNode permissionsTreeNode)
        {
            var permissionTreeNode = new TreeNode();
            switch (permission.Type)
            {
                case PermissionType.RolePermission:
                    var rolePermission = permission as RolePermission;
                    if (rolePermission != null)
                    {
                        permissionTreeNode.Text = String.Format(@"{0}[{1}]", Resource.TreeNode_RolePermission, rolePermission.Name);
                        permissionTreeNode.Name = Resource.TreeNode_RolePermission;
                        permissionTreeNode.Tag = new TreeNodeIndentity(TreeNodeType.InternalRolePermission, rolePermission.Id.ToString());
                    }
                    this.BuildInternalUserNamesNode(rolePermission, permissionTreeNode);
                    break;
                case PermissionType.UserPermission:
                    var userPermission = permission as UserPermission;
                    if (userPermission != null)
                    {
                        permissionTreeNode.Text = String.Format(@"{0}[{1}]", Resource.TreeNode_UserPermission, userPermission.User);
                        permissionTreeNode.Name = Resource.TreeNode_UserPermission;
                        permissionTreeNode.Tag = new TreeNodeIndentity(TreeNodeType.InternalUserPermission, userPermission.Id.ToString());
                    }
                    break;
                default:
                    break;
            }
            permissionsTreeNode.Nodes.Add(permissionTreeNode);
            return permissionTreeNode;
        }

        public TreeNode BuildInternalUserNamesNode(RolePermission rolePermission, TreeNode permissionTreeNode)
        {
            var userNamesTreeNode = new TreeNode()
            {
                Text = Resource.TreeNode_UserNames,
                Name = Resource.TreeNode_UserNames
            };
            if (rolePermission.UserNames != null)
            {
                foreach (var userName in rolePermission.UserNames)
                {
                    this.BuildInternalUserNameNode(userName, userNamesTreeNode, rolePermission.Id);
                }
            }
            permissionTreeNode.Nodes.Add(userNamesTreeNode);
            return userNamesTreeNode;
        }

        public TreeNode BuildInternalUserNameNode(UserName userName, TreeNode userNamesTreeNode, Guid permissionId)
        {
            var userNameTreeNode = new TreeNode()
            {
                Text = String.Format(@"{0}[{1}]", Resource.TreeNode_UserName, userName.Name),
                Name = userName.Name,
                Tag = new TreeNodeIndentity(TreeNodeType.InternalRolePermissonSecurityUser, String.Format(@"{0}#{1}", permissionId, userName.Id))
            };
            userNamesTreeNode.Nodes.Add(userNameTreeNode);
            return userNameTreeNode;
        }

        #endregion

        #endregion

        #region Project

        public void BuildProjectsNode(TreeNode cruiseControlTreeNode)
        {
            if (this.CruiseControl.Projects != null)
            {
                foreach (var project in this.CruiseControl.Projects)
                {
                    this.BuildProjectNode(project, cruiseControlTreeNode);
                }
            }
        }

        public TreeNode BuildProjectNode(Project project, TreeNode cruiseControlTreeNode)
        {
            var projectTreeNode = new TreeNode()
            {
                Text = String.Format(@"{0}[{1}]", Resource.TreeNode_Project, project.Name),
                Name = Resource.TreeNode_Project,
                Tag = new TreeNodeIndentity(TreeNodeType.Project, project.Id.ToString())
            };
            this.BuildSourceControlNode(project, projectTreeNode);
            this.BuildSecurityNode(project, projectTreeNode);
            this.BuildTriggersNode(project, projectTreeNode);
            this.BuildTasksNode(project, projectTreeNode);
            this.BuildPublishersNode(project, projectTreeNode);
            cruiseControlTreeNode.Nodes.Add(projectTreeNode);
            return projectTreeNode;
        }

        #endregion

        #region Security

        public TreeNode BuildSecurityNode(Project project, TreeNode projectTreeNode)
        {
            TreeNode securityTreeNode = null;
            if (project.Security != null)
            {
                securityTreeNode = new TreeNode()
                {
                    Text = Resource.TreeNode_Security,
                    Name = Resource.TreeNode_Security,
                    Tag = new TreeNodeIndentity(TreeNodeType.ProjectSecurity, project.Id.ToString())
                };
                switch (project.Security.Type)
                {
                    case SecurityType.DefaultProjectSecurity:
                        this.BuildPermissionsNode(project, securityTreeNode);
                        break;
                    case SecurityType.InheritedProjectSecurity:
                        break;
                    case SecurityType.NullProjectSecurity:
                        break;
                    default:
                        break;
                }
                projectTreeNode.Nodes.Add(securityTreeNode);
            }
            return securityTreeNode;
        }

        public TreeNode BuildPermissionsNode(Project project, TreeNode securityTreeNode)
        {
            TreeNode permissionsTreeNode = null;
            if (project.Security.Permissions != null)
            {
                permissionsTreeNode = new TreeNode()
               {
                   Text = Resource.TreeNode_Permissions,
                   Name = Resource.TreeNode_Permissions
               };
                foreach (var permission in project.Security.Permissions)
                {
                    this.BuildPermissionNode(permission, permissionsTreeNode, project.Id);
                }
                securityTreeNode.Nodes.Add(permissionsTreeNode);
            }
            return permissionsTreeNode;
        }

        public TreeNode BuildPermissionNode(Permission permission, TreeNode permissionsTreeNode, Guid projectId)
        {
            var permissionTreeNode = new TreeNode();
            switch (permission.Type)
            {
                case PermissionType.RolePermission:
                    var rolePermission = permission as RolePermission;
                    if (rolePermission != null)
                    {
                        permissionTreeNode.Text = String.Format(@"{0}[{1}]", Resource.TreeNode_RolePermission, rolePermission.Name);
                        permissionTreeNode.Name = rolePermission.Name;
                        permissionTreeNode.Tag = new TreeNodeIndentity(TreeNodeType.ProjectRolePermission, String.Format(@"{0}#{1}", projectId, rolePermission.Id.ToString()));
                    }
                    this.BuildProjectUserNamesNode(rolePermission, permissionTreeNode, projectId);
                    break;
                case PermissionType.UserPermission:
                    var userPermission = permission as UserPermission;
                    if (userPermission != null)
                    {
                        permissionTreeNode.Text = String.Format(@"{0}[{1}]", Resource.TreeNode_RolePermission, userPermission.User);
                        permissionTreeNode.Name = userPermission.User;
                        permissionTreeNode.Tag = new TreeNodeIndentity(TreeNodeType.ProjectUserPermission, String.Format(@"{0}#{1}", projectId, userPermission.Id.ToString()));
                    }
                    break;
                default:
                    break;
            }
            permissionsTreeNode.Nodes.Add(permissionTreeNode);
            return permissionTreeNode;
        }

        public TreeNode BuildProjectUserNamesNode(RolePermission rolePermission, TreeNode permissionTreeNode, Guid projectId)
        {
            TreeNode userNamesTreeNode = null;
            if (rolePermission.UserNames != null)
            {
                userNamesTreeNode = new TreeNode()
                {
                    Text = Resource.TreeNode_UserNames,
                    Name = Resource.TreeNode_UserNames
                };
                foreach (var userName in rolePermission.UserNames)
                {
                    this.BuildProjectUserNameNode(userName, userNamesTreeNode, projectId);
                }
                permissionTreeNode.Nodes.Add(userNamesTreeNode);

            }
            return userNamesTreeNode;
        }

        public TreeNode BuildProjectUserNameNode(UserName userName, TreeNode userNamesTreeNode, Guid projectId)
        {
            var userNameTreeNode = new TreeNode()
            {
                Text = String.Format(@"{0}[{1}]", Resource.TreeNode_UserName, userName.Name),
                Name = userName.Name,
                Tag = new TreeNodeIndentity(TreeNodeType.ProjcectRolePermissonSecurityUser, String.Format(@"{0}#{1}", projectId, userName.Id))
            };
            userNamesTreeNode.Nodes.Add(userNameTreeNode);
            return userNameTreeNode;
        }

        #endregion

        #region Source Control

        public TreeNode BuildSourceControlNode(Project project, TreeNode projectTreeNode)
        {
            TreeNode sourceControlTreeNode = null;
            if (project.SourceControl != null)
            {
                sourceControlTreeNode = new TreeNode()
                                                {
                                                    Tag =
                                                        new TreeNodeIndentity(TreeNodeType.SourceControl,
                                                                              project.Id.ToString()),
                                                    Text =
                                                        String.Format(@"{0}[{1}]", Resource.TreeNode_SourceControl,
                                                                      project.SourceControl.Type),
                                                    Name = project.SourceControl.Type.ToString()
                                                };
                projectTreeNode.Nodes.Add(sourceControlTreeNode);
            }
            return sourceControlTreeNode;
        }

        #endregion

        #region Triggers

        public TreeNode BuildTriggersNode(Project project, TreeNode projectTreeNode)
        {
            TreeNode triggersTreeNode = null;
            if (project.Triggers != null && project.Triggers.Count != 0)
            {
                triggersTreeNode = new TreeNode()
                                           {
                                               Text = Resource.TreeNode_Triggers,
                                               Name = Resource.TreeNode_Triggers
                                           };
                foreach (var trigger in project.Triggers)
                {
                    this.BuildTriggerNode(trigger, triggersTreeNode, project.Id);
                }
                projectTreeNode.Nodes.Add(triggersTreeNode);
            }
            return triggersTreeNode;
        }

        public TreeNode BuildTriggerNode(Trigger trigger, TreeNode triggersTreeNode, Guid projectId)
        {
            TreeNode triggerTreeNode = null;
            if (trigger != null)
            {
                triggerTreeNode = new TreeNode();
                switch (trigger.Type)
                {
                    case TriggerType.IntervalTrigger:
                        triggerTreeNode.Text = Resource.TreeNode_IntervalTrigger;
                        triggerTreeNode.Name = Resource.TreeNode_IntervalTrigger;
                        break;
                    case TriggerType.CronTrigger:
                        triggerTreeNode.Text = Resource.TreeNode_CronTrigger;
                        triggerTreeNode.Name = Resource.TreeNode_CronTrigger;
                        break;
                    default:
                        break;
                }
                triggerTreeNode.Tag = new TreeNodeIndentity(TreeNodeType.Trigger, String.Format(@"{0}#{1}", projectId, trigger.Id));
                triggersTreeNode.Nodes.Add(triggerTreeNode);
            }
            return triggerTreeNode;
        }

        #endregion

        #region Tasks

        public TreeNode BuildTasksNode(Project project, TreeNode projectTreeNode)
        {
            TreeNode tasksTreeNode = null;
            if (project.Tasks != null && project.Tasks.Count != 0)
            {
                tasksTreeNode = new TreeNode() { Text = Resource.TreeNode_Tasks, Name = Resource.TreeNode_Tasks };
                foreach (var task in project.Tasks)
                {
                    this.BuildTaskNode(task, tasksTreeNode, project.Id);
                }
                projectTreeNode.Nodes.Add(tasksTreeNode);
            }
            return tasksTreeNode;
        }

        public TreeNode BuildTaskNode(Task task, TreeNode tasksTreeNode, Guid projectId)
        {
            TreeNode taskTreeNode = null;
            if (task != null)
            {
                taskTreeNode = new TreeNode();
                switch (task.Type)
                {
                    case TaskType.MSBuild:
                        taskTreeNode.Text = Resource.TreeNode_MSBuild;
                        taskTreeNode.Name = Resource.TreeNode_MSBuild;
                        taskTreeNode.Tag = new TreeNodeIndentity(TreeNodeType.MSBuildTask, String.Format(@"{0}#{1}", projectId, task.Id));
                        break;
                    case TaskType.NUnit:
                        taskTreeNode.Text = Resource.TreeNode_NUnit;
                        taskTreeNode.Name = Resource.TreeNode_NUnit;
                        taskTreeNode.Tag = new TreeNodeIndentity(TreeNodeType.NUnitTask, String.Format(@"{0}#{1}", projectId, task.Id));
                        this.BuildAssemblysNode(task as NUnit, taskTreeNode, projectId, task.Id);
                        break;
                    case TaskType.Exec:
                        taskTreeNode.Text = Resource.TreeNode_Exec;
                        taskTreeNode.Name = Resource.TreeNode_Exec;
                        taskTreeNode.Tag = new TreeNodeIndentity(TreeNodeType.ExecTask, String.Format(@"{0}#{1}", projectId, task.Id));
                        break;
                    default:
                        break;
                }
                tasksTreeNode.Nodes.Add(taskTreeNode);
            }
            return taskTreeNode;
        }

        public TreeNode BuildAssemblysNode(NUnit nunit, TreeNode taskTreeNode, Guid projectId, Guid taskId)
        {
            TreeNode assemblysTreeNode = null;
            if (nunit != null)
            {
                assemblysTreeNode = new TreeNode() { Text = Resource.TreeNode_Assemblies, Name = Resource.TreeNode_Assemblies };
                if (nunit.Assemblies != null)
                {
                    foreach (var assembly in nunit.Assemblies)
                    {
                        this.BuildAssemblyNode(assembly, assemblysTreeNode, projectId, taskId);
                    }
                }
                taskTreeNode.Nodes.Add(assemblysTreeNode);
            }
            return assemblysTreeNode;
        }

        public TreeNode BuildAssemblyNode(Assembly assembly, TreeNode assemblysTreeNode, Guid projectId, Guid taskId)
        {
            TreeNode assemblyTreeNode = null;
            if (assembly != null)
            {
                assemblyTreeNode = new TreeNode()
               {
                   Text = Resource.TreeNode_Assembly,
                   Name = Resource.TreeNode_Assembly,
                   ToolTipText = assembly.Text,
                   Tag = new TreeNodeIndentity(TreeNodeType.Assembly, String.Format(@"{0}#{1}#{2}", projectId, taskId, assembly.Id))
               };
                assemblysTreeNode.Nodes.Add(assemblyTreeNode);
            }
            return assemblyTreeNode;
        }

        #endregion

        #region Publishers

        public TreeNode BuildPublishersNode(Project project, TreeNode projectTreeNode)
        {
            TreeNode publishersTreeNode = null;
            if (project.Publishers != null && project.Publishers.Count != 0)
            {
                publishersTreeNode = new TreeNode() { Text = Resource.TreeNode_Publishers, Name = Resource.TreeNode_Publishers };
                foreach (var publisher in project.Publishers)
                {
                    switch (publisher.Type)
                    {
                        case PublisherType.XmlLogger:
                            this.BuildXmlLoggerNode(publisher, publishersTreeNode, project.Id);
                            break;
                        case PublisherType.Email:
                            this.BuildEmailNode(publisher, publishersTreeNode, project.Id);
                            break;
                        case PublisherType.ArtifactCleanup:
                            this.BuildArtifactCleanupNode(publisher, publishersTreeNode, project.Id);
                            break;
                        default:
                            break;
                    }
                }
                projectTreeNode.Nodes.Add(publishersTreeNode);
            }
            return publishersTreeNode;
        }

        public TreeNode BuildXmlLoggerNode(Publisher publisher, TreeNode publishersTreeNode, Guid projectId)
        {
            var xmlLoggerTreeNode = new TreeNode()
            {
                Text = Resource.TreeNode_XmlLogger,
                Name = Resource.TreeNode_XmlLogger,
                Tag = new TreeNodeIndentity(TreeNodeType.Publisher, String.Format(@"{0}#{1}", projectId, publisher.Id))
            };
            publishersTreeNode.Nodes.Add(xmlLoggerTreeNode);
            return xmlLoggerTreeNode;
        }

        public TreeNode BuildEmailNode(Publisher publisher, TreeNode publishersTreeNode, Guid projectId)
        {
            TreeNode emailTreeNode = null;
            if (publisher != null)
            {
                emailTreeNode = new TreeNode()
               {
                   Text = Resource.TreeNode_Email,
                   Name = Resource.TreeNode_Email,
                   Tag = new TreeNodeIndentity(TreeNodeType.EmailPublisher, String.Format(@"{0}#{1}", projectId, publisher.Id))
               };
                var email = publisher as Email;
                this.BuildUsersNode(email, emailTreeNode, projectId);
                this.BuildGroupsNode(email, emailTreeNode, projectId);
                this.BuildModifierNotificationTypesNode(email, emailTreeNode);
                this.BuildSubjectSettingsNode(email, emailTreeNode, projectId);
                this.BuildXslFilesNode(email, emailTreeNode, projectId);
                publishersTreeNode.Nodes.Add(emailTreeNode);
            }
            return emailTreeNode;
        }

        public TreeNode BuildUsersNode(Email email, TreeNode emailTreeNode, Guid projectId)
        {
            TreeNode usersTreeNode = null;
            if (email != null)
            {
                usersTreeNode = new TreeNode() { Text = Resource.TreeNode_Users, Name = Resource.TreeNode_Users };
                if (email.Users != null)
                {
                    foreach (var user in email.Users)
                    {
                        this.BuildUserNode(user, usersTreeNode, projectId, email.Id);
                    }
                }
                emailTreeNode.Nodes.Add(usersTreeNode);
            }
            return usersTreeNode;
        }

        public TreeNode BuildUserNode(User user, TreeNode usersTreeNode, Guid projectId, Guid publisherId)
        {
            TreeNode userTreeNode = null;
            if (user != null)
            {
                userTreeNode = new TreeNode()
               {
                   Text = String.Format(@"{0}[{1}]", Resource.TreeNode_User, user.Name),
                   Name = user.Name,
                   Tag = new TreeNodeIndentity(TreeNodeType.EmailUser, String.Format(@"{0}#{1}#{2}", projectId, publisherId, user.Id))
               };
                usersTreeNode.Nodes.Add(userTreeNode);
            }
            return userTreeNode;
        }

        public TreeNode BuildGroupsNode(Email email, TreeNode emailTreeNode, Guid projectId)
        {
            TreeNode groupsTreeNode = null;
            if (email != null)
            {
                groupsTreeNode = new TreeNode()
                                     {
                                         Text = Resource.TreeNode_Groups,
                                         Name = Resource.TreeNode_Groups,
                                         Tag = new TreeNodeIndentity(TreeNodeType.EmailGroups, String.Format(@"{0}#{1}", projectId, email.Id))
                                     };
                if (email.Groups != null)
                {
                    foreach (var group in email.Groups)
                    {
                        this.BuildGroupNode(group, groupsTreeNode, projectId, email.Id);
                    }
                }
                emailTreeNode.Nodes.Add(groupsTreeNode);
            }
            return groupsTreeNode;
        }

        public TreeNode BuildGroupNode(Group group, TreeNode groupsTreeNode, Guid projectId, Guid publisherId)
        {
            TreeNode groupTreeNode = null;
            if (group != null)
            {
                groupTreeNode = new TreeNode()
                {
                    Text = String.Format(@"{0}[{1}]", Resource.TreeNode_Group, group.Name),
                    Name = group.Name,
                    Tag = new TreeNodeIndentity(TreeNodeType.EmailGroup, String.Format(@"{0}#{1}#{2}", projectId, publisherId, group.Id))
                };
                this.BuildNotificationsNode(group, groupTreeNode, projectId);
                groupsTreeNode.Nodes.Add(groupTreeNode);
            }
            return groupTreeNode;
        }

        public TreeNode BuildNotificationsNode(Group group, TreeNode groupTreeNode, Guid projectId)
        {
            TreeNode notificationsTreeNode = null;
            if (group != null)
            {
                notificationsTreeNode = new TreeNode()
               {
                   Text = Resource.TreeNode_Notifications,
                   Name = Resource.TreeNode_Notifications
               };
                if (group.Notifications != null)
                {
                    foreach (var notificationType in group.Notifications)
                    {
                        this.BuildNotificationTypeNode(notificationType, notificationsTreeNode);
                    }
                }
                groupTreeNode.Nodes.Add(notificationsTreeNode);
            }
            return notificationsTreeNode;
        }

        public TreeNode BuildModifierNotificationTypesNode(Email email, TreeNode emailTreeNode)
        {
            TreeNode modifierNotificationTypesTreeNode = null;
            if (email != null)
            {
                modifierNotificationTypesTreeNode = new TreeNode()
               {
                   Text = Resource.TreeNode_ModifierNotificationTypes,
                   Name = Resource.TreeNode_ModifierNotificationTypes
               };
                if (email.ModifierNotificationTypes != null)
                {
                    foreach (var notificationType in email.ModifierNotificationTypes)
                    {
                        this.BuildNotificationTypeNode(notificationType, modifierNotificationTypesTreeNode);
                    }
                }
                emailTreeNode.Nodes.Add(modifierNotificationTypesTreeNode);
            }
            return modifierNotificationTypesTreeNode;
        }

        public TreeNode BuildNotificationTypeNode(NotificationType notificationType, TreeNode notificationsTreeNode)
        {
            var notificationTypeTreeNode = new TreeNode()
            {
                Text = String.Format(@"{0}[{1}]", Resource.TreeNode_NotificationType, notificationType),
                Name = String.Format(@"{0}[{1}]", Resource.TreeNode_NotificationType, notificationType),
            };
            notificationsTreeNode.Nodes.Add(notificationTypeTreeNode);
            return notificationTypeTreeNode;
        }

        public TreeNode BuildSubjectSettingsNode(Email email, TreeNode emailTreeNode, Guid projectId)
        {
            TreeNode subjectSettingsTreeNode = null;
            if (email != null)
            {
                subjectSettingsTreeNode = new TreeNode()
               {
                   Text = Resource.TreeNode_SubjectSettings,
                   Name = Resource.TreeNode_SubjectSettings
               };
                if (email.SubjectSettings != null)
                {
                    foreach (var subject in email.SubjectSettings)
                    {
                        this.BuildSubjectNode(subject, subjectSettingsTreeNode, projectId, email.Id);
                    }
                }
                emailTreeNode.Nodes.Add(subjectSettingsTreeNode);
            }
            return subjectSettingsTreeNode;
        }

        public TreeNode BuildSubjectNode(Subject subject, TreeNode subjectSettingsTreeNode, Guid projectId, Guid emailId)
        {
            TreeNode subjectTreeNode = null;
            if (subject != null)
            {
                subjectTreeNode = new TreeNode()
               {
                   Text = Resource.TreeNode_Subject,
                   Name = Resource.TreeNode_Subject,
                   ToolTipText = String.Format(@"{0}[{1}]", subject.BuildResultType, subject.Value),
                   Tag = new TreeNodeIndentity(TreeNodeType.Subject, String.Format(@"{0}#{1}#{2}", projectId, emailId, subject.Id))
               };
                subjectSettingsTreeNode.Nodes.Add(subjectTreeNode);
            }
            return subjectTreeNode;
        }

        public TreeNode BuildXslFilesNode(Email email, TreeNode emailTreeNode, Guid projectId)
        {
            TreeNode xslFilesTreeNode = null;
            if (email != null)
            {
                xslFilesTreeNode = new TreeNode() { Text = Resource.TreeNode_XslFiles, Name = Resource.TreeNode_XslFiles };
                if (email.XslFiles != null)
                {
                    foreach (var file in email.XslFiles)
                    {
                        this.BuildFileNode(file, xslFilesTreeNode, projectId, email.Id);
                    }
                }
                emailTreeNode.Nodes.Add(xslFilesTreeNode);
            }
            return xslFilesTreeNode;
        }

        public TreeNode BuildFileNode(XslFile file, TreeNode xslFilesTreeNode, Guid projectId, Guid emailId)
        {
            TreeNode fileTreeNode = null;
            if (file != null)
            {
                fileTreeNode = new TreeNode()
               {
                   Text = Resource.TreeNode_File,
                   Name = Resource.TreeNode_File,
                   ToolTipText = file.Text,
                   Tag = new TreeNodeIndentity(TreeNodeType.XslFile, String.Format(@"{0}#{1}#{2}", projectId, emailId, file.Id))
               };
                xslFilesTreeNode.Nodes.Add(fileTreeNode);
            }
            return fileTreeNode;
        }

        public TreeNode BuildArtifactCleanupNode(Publisher publisher, TreeNode publishersTreeNode, Guid projectId)
        {
            var artifactCleanupTreeNode = new TreeNode()
            {
                Text = Resource.TreeNode_ArtifactCleanup,
                Name = Resource.TreeNode_ArtifactCleanup,
                Tag = new TreeNodeIndentity(TreeNodeType.Publisher, String.Format(@"{0}#{1}", projectId, publisher.Id))
            };
            publishersTreeNode.Nodes.Add(artifactCleanupTreeNode);
            return artifactCleanupTreeNode;
        }

        #endregion
    }
}
